<!--
 * @Description: 变更重要程度
 * @Author: wind-lc
 * @version: 1.0
 * @Date: 2021-09-22 17:08:29
 * @LastEditTime: 2021-09-23 19:33:50
 * @FilePath: \cscec-pms-admin\src\views\project\lists\components\ChangeCmportanceOrCode.vue
-->
<template>
  <base-modal
    :title="title"
    :closable="true"
    :visible="visible"
    destroy-on-close
    width="900px"
    ok-text="提交"
    :confirm-loading="confirmLoading"
    @cancel="handleClose"
    @ok="handleSubmit"
  >
    <custom-form ref="form" :is-validate="true" :edit-model="''" :rules="rules" :form="form" :form-list="formOption" />
  </base-modal>
</template>
<script>
import BaseModal from '@/components/BaseModal'
import { getImportantsList } from '@/api/project/importants'
import { projectUpdate } from '@/api/project/projects'
export default {
  name: 'ChangeCmportanceOrCode',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    type: {
      type: Number,
      require: true
    },
    data: {
      type: Object,
      require: true
    }
  },
  components: {
    BaseModal
  },
  data() {
    return {
      // 标题
      title: '',
      // 表单
      form: {
        projectImportanceId: '',
        bureauQualityCode: '',
        yunZhuProjectCode: ''
      },
      //下拉列表
      list: [],
      // 提交加载
      confirmLoading: false
    }
  },
  computed: {
    // 表单配置
    formOption() {
      const option = [
        {
          label: '项目重要程度',
          keyName: 'projectImportanceId',
          type: 'a-select',
          data: this.list,
          span: 12,
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          label: '局质量系统编码',
          keyName: 'bureauQualityCode',
          type: 'BaseInput',
          span: 12,
          rules: [{ required: true, message: '请输入', trigger: 'change' }]
        },
        {
          label: '云筑项目编码',
          keyName: 'yunZhuProjectCode',
          type: 'BaseInput',
          span: 12,
          rules: [{ required: true, message: '请输入', trigger: 'change' }]
        }
      ]
      return [option[this.type]]
    },
    // 校验
    rules() {
      const obj = {}
      this.formOption.map((item) => {
        if (item.rules) {
          obj[item.keyName] = item.rules
        }
      })
      return obj
    }
  },
  watch: {
    visible: {
      handler(val) {
        if (val) {
          if (this.type === 0) {
            // 变更重要程度
            this.title = '变更重要程度'
            this.form.projectImportanceId = this.data.projectImportanceId
          }
          if (this.type === 1) {
            // 局质量系统编码
            this.title = '局质量系统编码'
            this.form.bureauQualityCode = this.data.bureauQualityCode
          }
          if (this.type === 2) {
            // 云筑项目编码
            this.title = '云筑项目编码'
            this.form.yunZhuProjectCode = this.data.yunZhuProjectCode
          }
        }
      }
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    /**
     * @description: 获取列表
     * @param {void}
     * @return {void}
     */
    getData() {
      getImportantsList().then(({ code, data }) => {
        if (code === 200) {
          this.list = data.map((el) => {
            return {
              value: el.importantId,
              label: el.importantName
            }
          })
        } else {
          this.list = []
        }
      })
    },
    /**
     * @description: 关闭
     * @param {void}
     * @return {void}
     */
    handleClose() {
      this.$emit('update:visible', false)
      this.form = {
        projectImportanceId: '',
        bureauQualityCode: '',
        yunZhuProjectCode: ''
      }
    },
    /**
     * @description: 更新
     * @param {void}
     * @return {void}
     */
    handleUpdate() {
      this.$emit('update', true)
    },
    /**
     * @description: 提交
     * @param {void}
     * @return {void}
     */
    handleSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.confirmLoading = true
          let params = {}
          if (this.type === 0) {
            params = {
              projectId: this.data.projectId,
              projectImportanceId: this.form.projectImportanceId
            }
          }
          if (this.type === 1) {
            params = {
              bureauQualityCode: this.form.bureauQualityCode,
              projectId: this.data.projectId
            }
          }
          if (this.type === 2) {
            params = {
              yunZhuProjectCode: this.form.yunZhuProjectCode,
              projectId: this.data.projectId
            }
          }
          projectUpdate(params)
            .then(({ data: { code } }) => {
              if (code === 200) {
                this.$message.success('操作成功')
                if (this.type === 2) {
                  this.data.yunZhuProjectCode = this.form.yunZhuProjectCode
                }
                this.handleClose()
                this.handleUpdate()
              }
            })
            .finally(() => {
              this.confirmLoading = false
            })
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
</style>
